package com.jsnkj.basic.fragment

import android.os.Bundle
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.jsnkj.basic.R
import com.jsnkj.basic.adapter.OutcomeAdapter
import com.jsnkj.basic.bean.IncomeExpenditureEntity
import com.jsnkj.basic.databinding.FragmentWithdrawBinding
import com.jsnkj.basic.mvvm.BaseMVVMActivity
import com.jsnkj.basic.mvvm.BaseMVVMFragment
import com.jsnkj.basic.util.ErrorResultUtil

/**
 * 提现
 */
class WithdrawFragment : BaseMVVMFragment<FragmentWithdrawBinding, WithdrawViewModel>() {

    companion object {
        fun newInstance() = WithdrawFragment()
    }

    override val layoutId: Int
        get() = R.layout.fragment_withdraw


    var outcomeAdapter: OutcomeAdapter? = null

    override fun initView(savedInstanceState: Bundle?) {
        mVM.errorResult.observe(this) {

            ErrorResultUtil.errorResult(requireActivity() as BaseMVVMActivity<*, *>,it)

            mVDB.rvOutcome.visibility = View.GONE
            mVDB.linTotal.visibility = View.GONE
            mVDB.tvError.visibility = View.VISIBLE
        }

        mVM.postHttpResult.observe(this) {

            mVDB.tvTootal.text = "${it.total}元"
            initRecyclerView(it.list)
        }

        mVM.postWalletOutcomes()
    }


    private fun initRecyclerView(list: List<IncomeExpenditureEntity>?) {

        if (list.isNullOrEmpty()) {
            return
        }

        if (null == outcomeAdapter) {
            outcomeAdapter = OutcomeAdapter(requireActivity(), list)
        }


        mVDB.rvOutcome.apply {
            layoutManager =
                LinearLayoutManager(requireActivity(), LinearLayoutManager.VERTICAL, false)
            adapter = outcomeAdapter
        }

        mVDB.rvOutcome.visibility = View.VISIBLE
        mVDB.linTotal.visibility = View.VISIBLE
        mVDB.tvError.visibility = View.GONE
    }

}


